﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;
using SpinningSearch;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Collections.Concurrent;

namespace Test_ProgressBar__BackgroundWorker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        BackgroundWorker bw = new BackgroundWorker();

        public MainWindow()
        {
            InitializeComponent();
            bw.WorkerReportsProgress = true;
            bw.DoWork += bw_DoWork;
            bw.ProgressChanged += bw_ProgressChanged;
            bw.RunWorkerCompleted += bw_RunWorkerCompleted;
            bw.RunWorkerAsync();
        }


        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            CanneSpigolaCosta test = new CanneSpigolaCosta();
            List<string> csc = test.LoadAllLinks();
            ConcurrentDictionary<string, string> concurrentDictHTML = new ConcurrentDictionary<string, string>();

            progressBar1.Minimum = 0;
            progressBar1.Maximum = csc.Count;

            textBlock1.Text = "Ricerca in corso: " + Environment.NewLine + "CANNE DA SPIGOLA DALLA COSTA";

            Parallel.ForEach(csc, url =>
            {
                int actualPercentage;

                using (var response = WebRequest.Create(url).GetResponse())
                {
                    StreamReader reader = null;
                    string resultHTML;
                    reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    resultHTML = reader.ReadToEnd();
                    concurrentDictHTML.TryAdd(url, resultHTML);
                    actualPercentage = (int)((progressBar1.Value / progressBar1.Maximum) * 100);
                    bw.ReportProgress(actualPercentage);
                }
            }
            );


            //for (int i = 0; i < 100; i++)
            //{
            //    Thread.Sleep(100);
            //    bw.ReportProgress(i);
            //}
        }

        private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value += 1;
            //progressBar1.Value = e.ProgressPercentage;
            lblStatus.Content = "Percentuale di completamento: " + e.ProgressPercentage + " %";//, min= " + dlg.progressBar.Minimum + ", MAX=" + dlg.progressBar.Maximum;
            textBlock1.Text = e.ProgressPercentage.ToString();
        }


        public void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Value = 100;
            textBlock1.Text = "100";
        }
    }
}
